package com.simple.api.controller;

import com.simple.api.base.BaseController;
import com.simple.api.base.entity.ResponseModel;
import com.simple.api.base.enums.StateCode;
import com.simple.api.entity.Person;
import com.simple.api.serivce.FileDownloadService;
import com.simple.api.serivce.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

@RequestMapping("/api")
@RestController
public class TestController extends BaseController {

    @Autowired
    public TestService testService;
    @Autowired
    public FileDownloadService fileDownloadService;

    @GetMapping("queryPerson")
    public ResponseModel queryPerson() throws Exception{

        return new ResponseModel(StateCode.SUCCESS.code(),"",testService.queryAll());
    }
    @GetMapping("queryLogs")
    public ResponseModel queryLogs() throws Exception{

        return new ResponseModel(StateCode.SUCCESS.code(),"",testService.queryLogAll());
    }
    /**
     * 表单提交
     * @param person
     * @return
     * @throws Exception
     */
    @PostMapping("addPerson")
    public ResponseModel addPerson(Person person) throws Exception{
        testService.addPerson(person);
        return new ResponseModel(StateCode.SUCCESS.code(),"success",null);
    }

    @GetMapping("downApk")
    public void downApk(@RequestParam(name = "fileName") String fileName, HttpServletResponse response) throws Exception{
        fileDownloadService.downApkByStream(fileName,response);
    }
    @RequestMapping(value = "/download", method = RequestMethod.GET, produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public Object downLoad(@RequestParam(name = "fileName") String fileName) throws Exception{
        return fileDownloadService.downApkByResponseEntity(fileName);
    }

}

